<script setup>
import { ref } from "vue";
import { customerPasswordUpdateService } from "@/api/Customer";
import { ElMessage } from "element-plus";
const customerPassword = ref({
    oldPassword: '',
    newPassword: '',
    rePassword: '',
});
const checkNewpassword = (rule, value, callback) => {
    if(value === customerPassword.value.oldPassword) {
        callback(new Error('请确保修改后的密码和原密码不一样'));
    }else {
        callback();
    }
}
const checkRepassword = (rule, value, callback) => {
    if(value !== customerPassword.value.newPassword) {
        callback(new Error('请确保两次输入的密码一致'));
    }else {
        callback();
    }
}
const rules = {
  oldPassword: [
    { required: true, message: "请输入原密码", trigger: "blur" },
    {
      pattern: /^\S{6,20}$/,
      message: "密码必须是6-20位字符串",
      trigger: "blur",
    },
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    {
      pattern: /^\S{6,20}$/,
      message: "密码必须是6-20位字符串",
      trigger: "blur",
    },
    {
        validator: checkNewpassword,
    }
  ],
  rePassword: [
    { required: true, message: "请再次输入密码", trigger: "blur",},
    {
      pattern: /^\S{6,20}$/,
      message: "密码必须是6-20位字符串",
      trigger: "blur",
    },
    {
        validator: checkRepassword,
    },
  ],
};

//修改密码函数
const updateCustomerPassword = async () => {
  const passwordMap = new Map([
    ['old_pwd', customerPassword.value.oldPassword],
    ['new_pwd', customerPassword.value.newPassword],
    ['re_pwd', customerPassword.value.rePassword],
  ]);
  const result = await customerPasswordUpdateService(passwordMap);
  ElMessage.success(result.message ? result.message : "修改成功");
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form
          :model="customerPassword"
          :rules="rules"
          label-width="140px"
          size="large"
        >
          <el-form-item label="请输入原密码" prop="oldPassword">
            <el-input v-model="customerPassword.oldPassword" type="password"></el-input>
          </el-form-item>
          <el-form-item label="请输入新密码" prop="newPassword">
            <el-input v-model="customerPassword.newPassword" type="password"></el-input>
          </el-form-item>
          <el-form-item label="请再次输入密码" prop="rePassword">
            <el-input v-model="customerPassword.rePassword" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updateCustomerPassword"
              >确认重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>